package com.jcfk.eam.controller.main;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.enums.AvailableStatus;
import com.jcfk.common.business.enums.DeleteFlag;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.core.utils.IdUtils;
import com.jcfk.common.web.domain.*;

import com.jcfk.eam.domain.query.main.EamPointCheckOrderQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import com.jcfk.eam.domain.query.base.EamRepairPlanQuery;
import com.jcfk.eam.domain.dto.base.EamRepairPlanDTO;
import com.jcfk.eam.service.main.EamRepairPlanService;

import java.util.List;

/**
 * 计划维修
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-08-21
 */
@Api(tags = {"计划维修管理"})
@RestController
@RequestMapping("eamrepairplan")
public class EamRepairPlanController {
    @Autowired
    private EamRepairPlanService eamRepairPlanService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamRepairPlanQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询计划维修")
    @PostMapping("/queryByPage")
    public RetPage<EamRepairPlanDTO> queryByPage(@RequestBody ReqPageParam<EamRepairPlanQuery> requestParam) {
        if(requestParam.getSort().getOrderItem()==null){
            ReqSortParam<EamRepairPlanQuery> sortParam=new ReqSortParam();
            sortParam.setColumn("lastUpdateTime");
            sortParam.setOrderBy("desc");
            requestParam.setSort(sortParam);
        }
        DataPage<EamRepairPlanDTO> page = new DataPage<EamRepairPlanDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
                EamRepairPlanDTO.class);
        page = eamRepairPlanService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 计划维修
     * 根据id 查询详情
     */
    @ApiOperation("查询计划维修")
    @GetMapping("/get/{id}")
    public RetResult<EamRepairPlanDTO> get(@PathVariable("id") String id) {
        EamRepairPlanDTO item = eamRepairPlanService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理计划维修
     */
    @ApiOperation("保存计划维修")
    @PostMapping("/save")
    @RequestLogHeader(modular = "计划维修管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamRepairPlanDTO eamRepairPlan) {
        // 数据校验
        RetResult ret = eamRepairPlanService.isValid(eamRepairPlan);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(eamRepairPlan.getId())) {
            // 新增
            eamRepairPlanService.insert(eamRepairPlan);
        } else {
            // 更新
            eamRepairPlanService.update(eamRepairPlan);
        }
        return RetResult.ok();

    }

    /**
     * 批量删除计划维修
     */
    @ApiOperation("批量删除计划维修")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "计划维修管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamRepairPlanDTO eamRepairPlan) {
        return RetResult.ok(eamRepairPlanService.logicDeleteList(eamRepairPlan));
    }


    /**
     * 删除计划维修
     */
    @ApiOperation("删除计划维修")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "计划维修管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamRepairPlanService.deleteItem(id);
        return RetResult.ok();
    }

    /**
     * 生成工单
     */
    @ApiOperation("生成工单")
    @PostMapping("/generateOrder")
    public RetResult<?> generateOrder(@RequestBody EamRepairPlanDTO dto) {
        eamRepairPlanService.generateOrder(dto.getId());
        return RetResult.ok();
    }


    /**
     * 列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamRepairPlanQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("查询计划维修列表")
    @PostMapping("/getList")
    public RetResult<List<EamRepairPlanDTO>> getList(@RequestBody EamRepairPlanQuery requestParam) {
        if (requestParam.getDeleted() == null){
            requestParam.setDeleted(DeleteFlag.NOTDELETED.getValue());
        }
        if (requestParam.getEnabled() == null){
            requestParam.setEnabled(AvailableStatus.ENABLE.getValue());
        }

        return RetResult.ok(eamRepairPlanService.getList(requestParam));
    }
}
